<?php

declare(strict_types=1);

namespace App\Controller\m1;

use Throwable;
use App\Helper\Log;
use App\Helper\Util;
use App\Helper\Common;
use App\Helper\Verify;
use Hyperf\DbConnection\Db;
use App\Constants\ReturnCode;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\Manager\AuthMiddleware;
use App\Service\PlaceService;

#[Controller]
#[Middleware(AuthMiddleware::class)]
class PlaceController extends AbstractController
{
    #[Inject]
    protected PlaceService $placeServ;
    #城市场馆列表
    #[RequestMapping(path: "list", methods: "post")]
    public function list()
    {
        $province_id = $this->request->input('province_id', 0);
        $city_id = $this->request->input('city_id', 0);
        $currentPage = $this->request->input('currentPage', 1);
        $pageSize = $this->request->input('pageSize', 15);
        $where = [['is_del', '=', 0]];
        if (!empty($province_id)) {
            $where[] = ['province_id', '=', $province_id];
        }
        if (!empty($city_id)) {
            $where[] = ['city_id', '=', $city_id];
        }
        $offset = ($currentPage - 1) * $pageSize;
        $columns = [
            'id',
            'name',
            'province_id',
            'city_id',
            'sort',
            'status',
            'created_at',
            'updated_at',
        ];
        $query = Db::table('place')
            ->select($columns)
            ->where($where);
        $total = $query->count();
        $datas = $query->offset($offset)
            ->limit($pageSize)
            ->orderBy('sort', 'desc')
            ->orderBy('created_at', 'desc')
            ->get()
            ->toArray();
        $return_list = [];
        if(!empty($datas)){
            $statusArr = ["1"=>'显示', "0"=>'隐藏'];
            foreach ($datas as $data) {
                $province_name = Db::table('conf_provinces')
                    ->where(['id'=>$data['province_id']])
                    ->value("name");
                $city_name = Db::table('conf_provinces')
                    ->where(['id'=>$data['city_id']])
                    ->value("name");
                $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
                $return_list[] = [
                    'id'=>$data['id'],
                    'name'=>$data['name'],
                    'province_id'=>$data['province_id'],
                    'province'=>$province_name,
                    'city_id'=>$data['city_id'],
                    'city'=>$city_name,
                    'sort'=>$data['sort'],
                    'status'=>$data['status'],
                    'status_ch'=>$status_ch,
                    'created_at'=>date("Y-m-d H:i:s", $data['created_at']),
                    'updated_at'=>date("Y-m-d H:i:s", $data['updated_at'])
                ];
            }
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', ['total'=>$total, 'list'=>$return_list]);
    }
    #城市场馆详情
    #[RequestMapping(path: "detail", methods: "post")]
    public function detail()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $columns = [
            'id',
            'name',
            'province_id',
            'city_id',
            'sort',
            'status'
        ];
        $data = Db::table('place')
            ->select($columns)
            ->where(['id'=>$id])
            ->first();
        $return_data = [];
        if(!empty($data)){
            $province_name = Db::table('conf_provinces')
                ->where(['id'=>$data['province_id']])
                ->value("name");
            $city_name = Db::table('conf_provinces')
                ->where(['id'=>$data['city_id']])
                ->value("name");
            $statusArr = ["1"=>'显示', "0"=>'隐藏'];
            $status_ch = !empty($statusArr[$data['status']])?$statusArr[$data['status']]:"-";
            $return_data = [
                'id'=>$data['id'],
                'name'=>$data['name'],
                'province_id'=>$data['province_id'],
                'province'=>$province_name,
                'city_id'=>$data['city_id'],
                'city'=>$city_name,
                'sort'=>$data['sort'],
                'status'=>$data['status'],
                'status_ch'=>$status_ch
            ];
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $return_data);
    }
    #城市场馆保存
    #[RequestMapping(path: "save", methods: "post")]
    public function save()
    {
        $id = $this->request->input('id', 0);
        $name = $this->request->input('name', '');
        $province_id = $this->request->input('province_id', 0);
        $city_id = $this->request->input('city_id', 0);
        $sort = $this->request->input('sort', 0);
        if(empty($name)){
            return $this->fmt_response(ReturnCode::para_miss,'名称不能为空');
        }
        if(empty($province_id) || empty($city_id)){
            return $this->fmt_response(ReturnCode::para_miss,'省市不能为空');
        }
        $params = [
            'id'=>$id,
            'name'=>$name,
            'province_id'=>$province_id,
            'city_id'=>$city_id,
            'sort'=>$sort,
            'status'=>1
        ];
        $res = $this->placeServ->placeSave($params);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #城市场馆删除
    #[RequestMapping(path: "del", methods: "post")]
    public function del()
    {
        $id = $this->request->input('id', 0);
        if(empty($id)){
            return $this->fmt_response(ReturnCode::para_miss,'ID不能为空');
        }
        $res = Db::table('place')
            ->where(['id'=>$id])
            ->update(['is_del'=>1,'updated_at'=>time()]);
        if(!$res){
            return $this->fmt_response(ReturnCode::deal_false,'失败');
        }
        return $this->fmt_response(ReturnCode::deal_succ, '成功');
    }
    #城市场馆搜索
    #[RequestMapping(path: "search", methods: "post")]
    public function search()
    {
        $name = $this->request->input('name', '');
        if(empty($name)){
            return $this->fmt_response(ReturnCode::deal_succ, '成功', []);
        }
        $where = [['is_del', '=', 0]];
        if (!empty($name)) {
            $where[] = ['name', 'like', "%".trim($name)."%"];
        }
        $columns = [
            'id',
            'name'
        ];
        $data = Db::table('place')
            ->select($columns)
            ->where($where)
            ->offset(0)
            ->limit(10)
            ->get();
        return $this->fmt_response(ReturnCode::deal_succ, '成功', $data);
    }
}